#include "PWM.h"
#include "systick.h"
#include "LED.h" 

void PWM_Init(void)
{
	 /* 开启时钟 */
    rcu_periph_clock_enable(BSP_PWM_RCU);   
    gpio_mode_set(BSP_PWM_PORT, GPIO_MODE_AF, GPIO_PUPD_NONE, BSP_PWM_PIN);  
    gpio_output_options_set(BSP_PWM_PORT,GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,BSP_PWM_PIN);    
    /* 配置IO为定时器的通道 */ 
    gpio_af_set(BSP_PWM_PORT, GPIO_AF_1, BSP_PWM_PIN);
	
	
	timer_parameter_struct timer_initpara; // 定义定时器结构体
	
	/* 开启时钟 */    
	rcu_periph_clock_enable(BSP_PWM_TIMER_RCU); // 开启定时器时钟    
	/* CK_TIMERx = 4 x CK_APB1  = 4x50M = 200MHZ */
	
	rcu_timer_clock_prescaler_config(RCU_TIMER_PSC_MUL4); // 配置定时器时钟        
	timer_deinit(BSP_PWM_TIMER); // 复位定时器 
	
	/* 配置定时器参数 */    
	timer_initpara.prescaler = 168 - 1; //  时钟预分频值  PSC_CLK= 168MHZ / 168 = 1MHZ      
	timer_initpara.alignedmode = TIMER_COUNTER_EDGE; // 边缘对齐    
	timer_initpara.counterdirection = TIMER_COUNTER_UP; // 向上计数    
	timer_initpara.period = 10000 - 1; // 周期   T = 10000  1MHZ = 10ms  f = 100HZ       
	timer_initpara.clockdivision = TIMER_CKDIV_DIV1; // 分频因子       
	timer_initpara.repetitioncounter = 0; // 重复计数器 0-255  
	timer_init(BSP_PWM_TIMER,&timer_initpara); // 初始化定时器
	
	/* 使能定时器 */
	timer_enable(BSP_PWM_TIMER);
	
	timer_oc_parameter_struct timer_ocinitpara;
	/* 初始化定时器通道输出参数 */
    timer_channel_output_struct_para_init(&timer_ocinitpara);
    /* 配置定时器通道输出功能 */
    timer_ocinitpara.outputstate  = TIMER_CCX_ENABLE;
    timer_ocinitpara.ocpolarity   = TIMER_OC_POLARITY_HIGH;


	/* 配置定时器输出功能 */
	timer_channel_output_config(BSP_PWM_TIMER,BSP_PWM_TIMER_CH,&timer_ocinitpara);
	
	/* 配置定时器通道输出脉冲值 */
	timer_channel_output_pulse_value_config(BSP_PWM_TIMER,BSP_PWM_TIMER_CH,5000 - 1);
	
	/* 配置定时器通道输出比较模式 */
	timer_channel_output_mode_config(BSP_PWM_TIMER,BSP_PWM_TIMER_CH,TIMER_OC_MODE_PWM0);
	
	/* 配置定时器通道输出影子寄存器 */
	timer_channel_output_shadow_config(BSP_PWM_TIMER,TIMER_CH_0,TIMER_OC_SHADOW_DISABLE);
	
	/* 自动重载使能 */ 
	timer_auto_reload_shadow_enable(BSP_PWM_TIMER);
}

void PWM_breathing_lamp(void)
{
    static uint16_t value = 0;    
    static uint8_t direct = 0;        
    if(direct == 0)    
    {        
        value += 300;        
        if(value > 10000)            
        direct = 1; // 改变方向    
    }else     
    {        
        value -= 300;         
        if(value <= 0)            
        direct = 0; // 改变方向    
    }    
    timer_channel_output_pulse_value_config(BSP_PWM_TIMER,BSP_PWM_TIMER_CH,value); // 配置定时器通道输出脉冲值    
    delay_1ms(50);


}
